﻿# coding: utf-8

from Command import CCommand

import Transformation
import SceneNode

class CCmdScaling(CCommand):
    def __init__(self, **p):
        """
        Default class contructor.
        
        Parameters:
        @param nodeRef - the weak reference of the scene node
        @param newScale - the scale value (sx, sy, sz)
        """
        
        CCommand.__init__(self, TD.CMD_SCALING, **p)

    def DoCmd(self):
        try:
            nodeRef = self._paramDict['nodeRef']
            sx, sy, sz = self._paramDict['newScale']
            
            node = nodeRef()
            coreHandle = node.GetCoreHandle()
            
            # save the old scaling
            self._sx, self._sy, self._sz = Transformation.GetRelativeScale(SceneNode._CTransformation(coreHandle))
            
            # set the new scaling
            Transformation.SetRelativeScale(SceneNode._CTransformation(coreHandle), sx, sy, sz)
            
        except:
            SHOW_TRACE()
            
    def UndoCmd(self):
        try:
            nodeRef = self._paramDict['nodeRef']
            node = nodeRef()
            coreHandle = node.GetCoreHandle()
            
            # restore the old scaling
            Transformation.SetRelativeScale(SceneNode._CTransformation(coreHandle), self._sx, self._sy, self._sz)
            
        except:
            SHOW_TRACE()


    def UpdateScale(self, dsx, dsy, dsz):
        try:
            nodeRef = self._paramDict['nodeRef']
            node = nodeRef()
            coreHandle = node.GetCoreHandle()

            # restore the old scaling
            sx, sy, sz = self._sx + dsx, self._sy + dsy, self._sz + dsz
            self._paramDict['newScale'] = (sx, sy, sz)
            Transformation.SetRelativeScale(SceneNode._CTransformation(coreHandle), sx, sy, sz)

        except:
            SHOW_TRACE()

        return True
